#!/bin/bash
{
	#////////////////////////////////////
	# DietPi Boot Script
	#
	#////////////////////////////////////
	# Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com
	#
	#////////////////////////////////////
	#
	# Info:
	# - Location: /boot/dietpi/boot
	# - Activates on boot from dietpi-boot.service
	#////////////////////////////////////

	# Import DietPi-Globals --------------------------------------------------------------
	. /boot/dietpi/func/dietpi-globals
	readonly G_PROGRAM_NAME='DietPi-Boot'
	G_INIT
	# Import DietPi-Globals --------------------------------------------------------------

	#/////////////////////////////////////////////////////////////////////////////////////
	# Globals
	#/////////////////////////////////////////////////////////////////////////////////////

	Wait_For_Valid_Network_Connection(){

		local mode=$(sed -n '/^[[:blank:]]*CONFIG_BOOT_WAIT_FOR_NETWORK=/{s/^[^=]*=//p;q}' /boot/dietpi.txt)
		[[ $mode == 0 ]] && return 0 # Mode 0: Do not wait for network

		local loops_left=10 # Default: Wait up to 10 seconds for network
		while (( $loops_left ))
		do

			G_DIETPI-NOTIFY 2 "Waiting for valid network connection before continuing boot | Mode=$mode"

			if [[ $(ip r l 0/0) ]]; then

				G_DIETPI-NOTIFY 0 'Valid network connection found'
				return 0

			fi

			[[ $mode == 2 ]] || ((loops_left--)) # Mode 2: Wait unlimited for network
			sleep 1

		done

		G_DIETPI-NOTIFY 1 'Waiting for valid network connection timed out'
		return 1

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Main Loop
	#/////////////////////////////////////////////////////////////////////////////////////
	#----------------------------------------------------------------
	# Wait for network
	# - network-online.target should be an alternative, but ifup@.service is Type=simple which means that hotplug interfaces might not be fully configured before the target is reached.
	Wait_For_Valid_Network_Connection
	#----------------------------------------------------------------
	# Network time sync
	# - Do not run before firstrun setup has finished, since related dietpi.txt settings are applied during first DietPi-Software run.
	(( $G_DIETPI_INSTALL_STAGE > 1 )) && /boot/dietpi/func/run_ntpd 1
	#----------------------------------------------------------------
	# Write network details to /run/dietpi/.network
	/boot/dietpi/func/obtain_network_details
	# - Make file world-writeable initially, required for DietPi-Login to update it as non-root user
	chmod 666 /run/dietpi/.network
	#----------------------------------------------------------------
	# Pre-installed image, 1st run
	if (( $G_DIETPI_INSTALL_STAGE == 10 )); then

		# Create swap file
		/boot/dietpi/func/dietpi-set_swapfile

		# Run survey
		/boot/dietpi/dietpi-survey 1

		# Continue with normal boot
		echo 2 > /boot/dietpi/.install_stage

	fi
	#-----------------------------------------------------------------------------------
	exit
	#-----------------------------------------------------------------------------------
}
